package stage_1_module_4.code.homework_5;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.List;

public class ServerThread extends Thread {
    private Socket s = null;
    private List<Socket> socketlist = null;

    @Override
    public void run() {
        ObjectInputStream is =  null;
        FileOutputStream fos = null;
        DataInputStream dis = null;
        DataOutputStream dos = null;
        try {
            is = new ObjectInputStream(s.getInputStream());

            while (true) {
                Message m = (Message) is.readObject();
                System.out.println("在线客户列表" + socketlist);
                if (m.getType() == 1) {
                    // 当客户端发来的内容为"bye"时，则聊天结束
                    if ("bye".equals(m.getMsg())) {
                        System.out.println("客户端" + s.getInetAddress() + "已下线！");
                        // 将下线的客户端从集合中移除
                        socketlist.remove(s);
                        break;
                    }
                    System.out.println(m.getName()+"说:"+m.getMsg());
                    for (Socket so : socketlist) {
                        if (so != s) {
                            ObjectOutputStream objectOutputStream1 = new ObjectOutputStream(so.getOutputStream());
                            objectOutputStream1.writeObject(m);
                        }
                    }
                } else if (m.getType() == 2) {
                    System.out.println(m.getName()+"发来文件:"+m.getMsg());
                    if ("bye".equals(m.getMsg())) {
                        System.out.println("客户端" + s.getInetAddress() + "已下线！");
                        // 将下线的客户端从集合中移除
                        socketlist.remove(s);
                        break;
                    }
                    dis = new DataInputStream(s.getInputStream());
                    int len = dis.readInt();
                    String fname = "./stage_1_module_4/code/homework_5/receive/"+m.getMsg();
                    fos = new FileOutputStream(fname);
                    byte[] b = new byte[len];
                    dis.read(b, 0, len);
                    fos.write(b);
                    fos.flush();
                    fos.close();
                    for (Socket so : socketlist) {
                        if (so != s) {
                            dos = new DataOutputStream(so.getOutputStream());

                            ObjectOutputStream objectOutputStream1 = new ObjectOutputStream(so.getOutputStream());
                            objectOutputStream1.writeObject(m);
                            objectOutputStream1.flush();

                            // 输出文件长度
                            dos.writeInt(len);
                            // 传输文件内容
                            dos.write(b);
                            dos.flush();
                        }
                    }
                }
            }
        } catch (IOException | ClassNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();

        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        

    }

    public ServerThread (Socket s, List<Socket> socketlist) {
        this.s = s;
        this.socketlist = socketlist;
    }

}
